﻿using System;
using ZX.Common;
using ZX.DBModule;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using ZX.EHR.Library;
using ZX.TR.Bussiness;
using ZX.TR.Library;

namespace ZX.TR.Web
{
    public partial class TrSubjectInfo : EditPage
    {
        BTrQuestionnaireSubject bts = new BTrQuestionnaireSubject();
        public string sid = "", flag = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ZX.TR.Web.TrSubjectInfo));
            sid = Request.QueryString["SID"];
            flag = Request.QueryString["flag"];
            hdnSID.Value = sid;
            if (!this.IsPostBack)
            {
                bindType();
                if (sid != "")//新增时初始化数据
                {
                    V_TrSubjectInfo vtsi = bts.GetSubjectInfoById(sid);
                    if (vtsi != null)
                    {
                        bindSelect(sid);
                        txtSubjectName.Text = vtsi.SubjectName;
                        txtAnswer.Text = vtsi.RefAnswer;
                        txtContent.Text = vtsi.SubjectContent;
                        ddlType.SelectedValue = vtsi.SubjectType.ToString();
                        ddlRole.SelectedValue = vtsi.SubjectAngle;
                        txtExplain.Text = vtsi.Explain;
                        if (this.ddlType.SelectedValue == "0661d140-0571-4dd3-9c5d-b5bc51ec16f5" || this.ddlType.SelectedValue == "1661d140-0571-4dd3-9c5d-b5bc51ec16f5")
                        {
                            this.pnlItems.Visible = true;
                        }
                        else
                        {
                            this.pnlItems.Visible = false;
                        }
                    }
                }
            }
        }

        //绑定类型
        private void bindType()
        {
            ddlType.DataSource = bts.GetAllTypeInfo();
            ddlType.DataTextField = "PhraseText";
            ddlType.DataValueField = "PhraseID";
            ddlType.DataBind();
            ddlRole.DataSource = bts.GetAllRoleInfo();
            ddlRole.DataTextField = "PhraseText";
            ddlRole.DataValueField = "PhraseValue";
            ddlRole.DataBind();

        }
        //绑定选项
        private void bindSelect(string gid)
        {
            TrSubjectItem tww = new ZX.TR.Library.TrSubjectItem();
            IList<ZX.TR.Library.TrSubjectItem> twwl = bts.GetEntitys<TrSubjectItem>("TrSubjectInfoID='" + CSVHelper.FmSQL(gid) + "' order by ItemCode");
            //工作项赋值
            if (twwl != null && twwl.Count > 0)
            {

                HdfVid.Value = twwl.Count.ToString();
                StringBuilder sb = new StringBuilder();
                foreach (TrSubjectItem ww in twwl)
                {

                    sb.Append(ww.ItemIndex + "|");
                    sb.Append(ww.ItemCode + "|");
                    sb.Append(ww.SunjectItem + "|");
                    sb.Append(ww.TrSubjectInfoID + "|");
                    sb.Append("^");
                }
                hdfWWValue.Value = sb.ToString();

                Page.ClientScript.RegisterStartupScript(this.GetType(), "Review", "ReviewForm()", true);
            }
        }
        protected string GetUserName(Guid uid)
        {
            ZX.EHR.Library.DatUser du = new ZX.EHR.Library.DatUser();
            BaseBussnies bdu = new BaseBussnies(du);
            IList<ZX.EHR.Library.DatUser> dul = bdu.GetEntitys<ZX.EHR.Library.DatUser>("Userid='" + uid + "'");
            return dul[0].UName;
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlType.SelectedValue == "0661d140-0571-4dd3-9c5d-b5bc51ec16f5" || this.ddlType.SelectedValue == "1661d140-0571-4dd3-9c5d-b5bc51ec16f5")
            {
                this.pnlItems.Visible = true;
            }
            else
            {
                this.pnlItems.Visible = false;
            }
        }

        #region AJAX方法区

        //检查人检测
        [AjaxPro.AjaxMethod]
        public string CheckUser(string users)
        {
            string[] us = users.Split(';');
            ZX.EHR.Library.DatUser du = new ZX.EHR.Library.DatUser();
            BaseBussnies bdu = new BaseBussnies(du);
            StringBuilder sbuid = new StringBuilder();

            foreach (string user in us)
            {
                if (user == "")
                    continue;
                //IList<ZX.EHR.Library.DatUser> dul = bdu.GetEntitys<ZX.EHR.Library.DatUser>("MessageType='公告' and IsPublic='是'");
                IList<ZX.EHR.Library.DatUser> dul = bdu.GetEntitys<ZX.EHR.Library.DatUser>("UName='" + user + "'");
                if (dul == null)//用户不存在
                {
                    sbuid.Append("检查人[" + user + "]不存在，请重新填写;");
                }
                else if (dul.Count > 1)//用户数大于1
                {
                    sbuid.Append("检查人[" + user + "]存在多个，请按【登录账号】填写;");
                }
                else
                {
                    sbuid.Append(dul[0].UserID + ";");
                }
            }
            return sbuid.ToString();
        }
        //保存数据
        [AjaxPro.AjaxMethod]
        public int SavePlan(int len, string sidd, string strDdlt, string strName, string strRole, string strContent, string strAnswer, string strExplain, string str)
        {
            if (str == "")
                str = "l^l";
            TrSubjectItem twp = new TrSubjectItem();
            ZX.TR.Library.TrSubjectInfo trsInfo = new ZX.TR.Library.TrSubjectInfo();
            DatUser du = (DatUser)Session["User"];
            trsInfo.SubjectName = strName;
            trsInfo.AddTime = DateTime.Now;
            trsInfo.AddUser = du.UserID;
            trsInfo.SubjectAngle = strRole;
            trsInfo.SubjectContent = strContent;
            trsInfo.SubjectType = new Guid(strDdlt);
            trsInfo.RefAnswer = strAnswer;
            trsInfo.Explain = strExplain;
            string[] strs = str.Split('^');
            if (sidd == "")
            {
                trsInfo.TrSubjectInfoID = Guid.NewGuid();   
                BaseBussnies bli = new BaseBussnies(trsInfo);
                bli.Insert();
                //单选、多选题才写入子表
                if (strDdlt == "0661d140-0571-4dd3-9c5d-b5bc51ec16f5" || strDdlt == "1661d140-0571-4dd3-9c5d-b5bc51ec16f5")
                {
                    for (int i = 1; i <= len; i++)
                    {
                        string strTemp = strs[i - 1];
                        string[] arrStr = strTemp.Split('|');
                        TrSubjectItem tsItem = new TrSubjectItem();
                        tsItem.AddTime = DateTime.Now;
                        tsItem.AddUser = du.UserID;
                        tsItem.ItemCode = arrStr[1];
                        tsItem.ItemIndex = i;
                        tsItem.SunjectItem = arrStr[2]; ;
                        tsItem.TrSubjectInfoID = trsInfo.TrSubjectInfoID;
                        tsItem.TrSubjectItemID = Guid.NewGuid();
                        BaseBussnies bbes = new BaseBussnies(tsItem);
                        bbes.Insert();
                    }
                }
               
            }
            else
            {
                trsInfo.TrSubjectInfoID = new Guid(sidd);
                TrSubjectItem tsItemTemp = new TrSubjectItem();
                tsItemTemp.TrSubjectInfoID = trsInfo.TrSubjectInfoID;
                BaseBussnies bbesTemp = new BaseBussnies(tsItemTemp);
                bbesTemp.delete("TrSubjectInfoID='" + trsInfo.TrSubjectInfoID + "'");
                if (strDdlt == "0661d140-0571-4dd3-9c5d-b5bc51ec16f5" || strDdlt == "1661d140-0571-4dd3-9c5d-b5bc51ec16f5")
                {
                    
                    for (int i = 1; i <= len; i++)
                    {
                        string strTemp = strs[i - 1];
                        string[] arrStr = strTemp.Split('|');
                        TrSubjectItem tsItem = new TrSubjectItem();
                        tsItem.AddTime = DateTime.Now;
                        tsItem.AddUser = du.UserID;
                        tsItem.ItemCode = arrStr[1];
                        tsItem.ItemIndex = i;
                        tsItem.SunjectItem = arrStr[2];
                        tsItem.TrSubjectInfoID = trsInfo.TrSubjectInfoID;
                        tsItem.TrSubjectItemID = Guid.NewGuid();
                        BaseBussnies bbes = new BaseBussnies(tsItem);
                        bbes.Insert();
                    }
                }
                
                BaseBussnies bli = new BaseBussnies(trsInfo);
                bli.Update();
            }

            ////插入从表信息
            //if (strs[1] != "" && strs[2] != "")
            //{
            //    string wwv = strs[2];
            //    string[] wvs = wwv.Split('^');
            //    //string[] chuid = strs[1].Split(';');
            //    StringBuilder sbd = new StringBuilder();//获取当前ID集
            //    for (int i = 0; i < wvs.Length - 1; i++)
            //    {
            //        string[] details = wvs[i].Split('|');
            //        ZX.TR.Library.TrWeekWork tww = new ZX.TR.Library.TrWeekWork();

            //        tww.TrWeekPlanID = twp.TrWeekPlanID;
            //        tww.PlanWork = details[1];
            //        tww.PreResult = details[2];
            //        tww.W1 = details[3];
            //        tww.W2 = details[4];
            //        tww.W3 = details[5];
            //        tww.W4 = details[6];
            //        tww.W5 = details[7];
            //        //tww.Checker = new Guid(chuid[i]);
            //        tww.WorkType = "N";
            //        tww.PlanSelf = details[8];
            //        if (details[0] == "")
            //        {
            //            tww.TrWeekWorkID = Guid.NewGuid();
            //            sbd.Append(tww.TrWeekWorkID + ";");
            //            BaseBussnies bi = new BaseBussnies(tww);
            //            bi.Insert();
            //        }
            //        else
            //        {
            //            sbd.Append(details[0] + ";");
            //            tww.TrWeekWorkID = new Guid(details[0]);
            //            BaseBussnies bu = new BaseBussnies(tww);
            //            bu.update();
            //        }
            //    }

            //    //删除部分从表信息
            //    ZX.TR.Library.TrWeekWork twd = new ZX.TR.Library.TrWeekWork();
            //    BaseBussnies bww = new BaseBussnies(twd);
            //    IList<ZX.TR.Library.TrWeekWork> twdl = bww.GetEntitys<ZX.TR.Library.TrWeekWork>("TrWeekPlanID='" + strs[0] + "' and WorkType='N'");
            //    string[] wwids = sbd.ToString().Split(';');
            //    if (twdl != null && (twdl.Count > (wwids.Length - 1)))
            //    {
            //        foreach (ZX.TR.Library.TrWeekWork v in twdl)
            //        {
            //            if (!sbd.ToString().Contains(v.TrWeekWorkID.ToString()))
            //            {
            //                bww.delete(v);
            //            }
            //        }
            //    }
            //}
            return 1;
        }


        #endregion
    }
}